--
-- Author: mm
-- Date: 2018-03-19 14:55:37
--
-- NewguildController
--

local NewguildView = import("..views.NewguildView")
local NewguildDonateView = import("..views.NewguildDonateView")
local NewguildTips = import("..views.NewguildTips")
local NewRareInfoView = require("app.newBag.views.NewRareInfoView")
local ElevatePeerageView = import("..views.ElevatePeerageView")
local DynastyPromoteView = import("..views.DynastyPromoteView")
local DynastyHistoryView = import("..views.DynastyHistoryView")
local NewguildEmperorBookView = import("..views.NewguildEmperorBookView")
local PeerageTips = import("..views.PeerageTips")
local AuctionKeyboardPanel = require("app.auction.views.AuctionKeyboardPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

local panelNum = 6 -- 标签页个数

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("NewguildModel")
    self._curId = 1 -- 默认主页
    self._managerPanelCurIdx = 1 -- 默认招募
    self._itemChangeHandler = UD:onChanged("bag", handler(self, self.updateItem))
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
    UD:unChanged("bag", self._itemChangeHandler)
end

function ClassRef:listEvent()
    return {
        "onMyGuildInfoUpdate",
    }
end

function ClassRef:onEvent(eventName, data)
    if "onMyGuildInfoUpdate" == eventName then -- 公会基础信息改变
        self:refreshGuildInfo()
    end
end




-------------------------------------------------------
function ClassRef:openView(data)
    if cc.isDead(self._view) then
        self._model:reqMyGuildInfo() -- 展开UI的时候重新拉取公会信息
        self._model:reqGuildMemberList( self._model:getGuildId() ) -- 展开UI的时候拉取一次成员列表
        UD.msdkData:recheckJoinMsdkGroupState() -- 查询 和群关系 --
        self._view = NewguildView.new(app.mvc.VIEWTYPE_BLUR, self._model:getMyGuildInfo())
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onSwitchPanel(handler(self, self.onSwitchPanel))
        self._view:onOperate(handler(self, self.onOperate))
    end
    self:addChild(self._view)
    self:refreshManagePanelVisible()
    
    local idx = data and data.tabIdx or 1
    self._itemType = data and tonumber(data.itemType)
    self._itemId = data and tonumber(data.itemId)

    self._view:show(idx)
    self:refreshNewApplyTips()
    self:refreshChallengeTips()
    UD:updateGuildNewPoint( self._model:_judgeNeedShowRedPoint() )
   
end
function ClassRef:clearView()
    if self._view then
        self._view = nil

        self._curId = 1
        self._managerPanelCurIdx = 1
    end
end

-------------------------------------------------------
function ClassRef:openDonateView(data)
    if cc.isDead( self._donateView ) then
        self._donateView = NewguildDonateView.new()
        self._donateView:onCloseView(handler(self, self.closeDonateView))
        self._donateView:onClearView(handler(self, self.clearDonateView))
        self._donateView:onOperate(handler(self, self.onOperate))
        self._view:addChild(self._donateView)
    end
    self:refreshDonateView()
    self._donateView:show()
end
function ClassRef:closeDonateView(data)
    if not cc.isDead( self._donateView ) then
        self._donateView:removeFromParent(true)
        self._donateView = nil
    end
end
function ClassRef:clearDonateView()
    if self._donateView then
        self._donateView = nil
    end
end
function ClassRef:refreshDonateView()
    if self._donateView and self._donateView:isVisible() then
        self._donateView:refreshContent( self._model:getMyGuildMemberInfo() )
    end
end


-------------------------------------------------------
function ClassRef:showGuildTips( titleStr, contentStr, pos )
    if cc.isDead( self._guildTips ) then
        self._guildTips = NewguildTips.create()

        self._view:addChild( self._guildTips )
        -- 点击空白区域关闭
        display.nodeRemoveForNotHit(self._guildTips, function()
            if self._guildTips then
                self._guildTips:removeFromParent(true)
                self._guildTips = nil
            end
        end)
    end
    self._guildTips:setNameStr( titleStr )
    self._guildTips:setContentStr( contentStr )
    self._guildTips:setVisible( true )

    local width = self._guildTips:getContentSize2()
    local posX = pos.x
    if posX + width > display.width then
        posX = display.width - width
    end
    self._guildTips:setPosition( cc.p( posX, pos.y ) )
end

function ClassRef:_showRareTips(data, pos)
    if cc.isDead( self._rareInfoPanel ) then
        self._rareInfoPanel = NewRareInfoView.create()
        self._rareInfoPanel:setData( data )
        self._view:addChild( self._rareInfoPanel )
        -- 点击空白区域关闭
        display.nodeRemoveForNotHit(self._rareInfoPanel, function()
            if self._rareInfoPanel then
                self._rareInfoPanel:performWithDelay(function()
                    self._rareInfoPanel:removeFromParent(true)
                    self._rareInfoPanel = nil
                end, 0)
            end
        end)
    end

    local width = self._rareInfoPanel:getContentSize2()
    local posX = pos.x
    if posX + width > display.width then
        posX = display.width - width
    end
    self._rareInfoPanel:setPosition( cc.p( posX, pos.y ) )
end


-------------------------------------------------------
function ClassRef:refreshGuildInfo()
    if not self:isViewAlive() then return end

    local panel = self._view:getPanelById( self._curId )
    if 1 == self._curId or 6 == self._curId then -- 主页 或者 管理panel的时候更新UI
        panel:refreshGuildInfo( self._model:getMyGuildInfo() )
    elseif 3 == self._curId then
        panel:refresh(self._model:getMyGuildInfo())
    elseif 2 == self._curId then
        panel:refreshGuildInfo( self._model:getMyGuildInfo(), self._model:getMyGuildMemberInfo(), self._model:getGuildTalents() )
    end

    self:refreshManagePanelVisible()
end

function ClassRef:refreshManagePanelVisible()
    if not self:isViewAlive() then return end
    self._view:refreshManagePanelVisible( self._model:isGuildManager() ) -- 刷新管理panel可见性
end

-- 刷新公会主页成员列表
-- shownOffLine-是否显示离线，不传时以HomePagePanel中状态为准
-- memberListTemp-指定显示列表
function ClassRef:refreshGuildMemberList( shownOffLine, memberListTemp )
    if not self:isViewAlive() then return end
    if 1 ~= self._curId then return end

    local shownFlag = nil
    if nil ~= shownOffLine then
        shownFlag = shownOffLine
    else
        shownFlag = self._view:getPanelById(1):isShownOffLineMember()
    end

    local listView = self._view:getPanelById(1):getGuildMemberList()

    local memberList = {}
    if memberListTemp then -- 指定了特定列表
        memberList = memberListTemp
    else
        local allMemberList = self._model:getGuildMembers()
        if shownFlag then
            memberList = allMemberList
        else
            for i,v in ipairs(allMemberList) do
                if v:isOnline() then
                    table.insert( memberList, v )
                end
            end
        end
    end

    listView:reloadData(memberList)
end

-- 刷新管理panel上新申请者标记
function ClassRef:refreshNewApplyTips()
    if not self:isViewAlive() then return end
    self._view:refreshNewApplyTips( self._model:getNewApplyTips() )

    if 6 == self._curId then
        self._view:getPanelById( self._curId ):refreshNewApplyTips( self._model:getNewApplyTips() )
        --self._model:reqGuildApplyList()
    end
end

--管理刷新有挑战宝箱领奖的tips
function ClassRef:refreshChallengeTips()
    if not self:isViewAlive() then return end
    self._view:refreshChallengeTips( self._model:getHaveChallengeBoxGet() )
end


-- 刷新公会申请者列表
function ClassRef:refreshGuildApplicantList( appliedList )
    if not self:isViewAlive() then return end
    if 6 ~= self._curId then return end

    local guildManagerPanel = self._view:getPanelById(6)
    if guildManagerPanel then
        guildManagerPanel:refreshApplyListData(appliedList)

        if #appliedList > 0 then
            guildManagerPanel:noApplicantTipsShown( false )
        else
            guildManagerPanel:noApplicantTipsShown( 2 == self._managerPanelCurIdx )
        end
    end
end


-- 刷新公会挑战列表
function ClassRef:refreshGuildChallengePanel( guildChallengeList )
    if not self:isViewAlive() then return end

    if 4 == self._curId then -- 处于挑战panel
        self._view:getPanelById( self._curId ):refreshContent( guildChallengeList )
    end
end


-- 1 主页
-- 2 研究
-- 3 飞艇
-- 4 挑战
-- 5 商店
-- 6 管理
function ClassRef:onSwitchPanel( idx )
    if not self:isViewAlive() then return end

    self._curId = idx
    local panel = self._view:getPanelById( idx )
    if idx == 1 then
        panel:refreshGuildInfo( self._model:getMyGuildInfo() )
        self:refreshGuildMemberList()
    elseif idx == 2 then
        panel:refreshGuildInfo( self._model:getMyGuildInfo(), self._model:getMyGuildMemberInfo() , self._model:getGuildTalents())
    elseif idx == 3 then
        panel:refresh(self._model:getMyGuildInfo())
    elseif idx == 4 then
        self._model:reqChallengeList()
    elseif idx == 5 then
    elseif idx == 6 then
        panel:refreshGuildInfo( self._model:getMyGuildInfo() )
        panel:refreshNewApplyTips( self._model:getNewApplyTips() )
        self._model:reqGuildApplyList()
    end
end

--激活公会天赋
function ClassRef:reqStudyGuildTalent(cost, talentId)
    local myGuildInfo = self._model:getMyGuildInfo()
    local haveMoney = myGuildInfo:getGuildFund()
    if haveMoney < cost then
        display.pushToast( L("guild_needgold") )
    elseif 0 >= myGuildInfo:getGuildLastSkillPoint()  then
        display.pushToast( L("guild_need_skillpoint") )
    else
        local confirmDialog = display.newConfirmDialog(L("guild_skillconfirm"),
            function() -- 确定
                --print("start send study msg id is "..talentId)
                self._model:reqActiveGuildTalent(talentId)
            end)
        UIHandler:addPopDialog( confirmDialog )
    end
end


-------------------------------------------------------
-- 扩充公会成员人数上限
function ClassRef:_upGuildMemberNumLimit()
    if self._model:canContinueUpLimitNum() then -- 可以继续升级?
        local cost = GD:getSystemPar( "guildUpperMemberLimitCost", 100 ) -- 扩充一次的花费
        local nextMaxNum = self._model:getGuildMemberMaxNum() + tonumber( GD:getSystemPar( "guildUpperMemberLimitNum", 5 ) ) -- 即将扩充到的人数上限
        local upMmeberNumLimitDialog = display.newConfirmDialog( string.format( L("guild_expand"), cost, nextMaxNum ),
            function() -- 确定
                self._model:reqGuildUpperMemberLimit()
            end)
        UIHandler:addPopDialog( upMmeberNumLimitDialog )
    else
        local messageBox = display.newMessageBox( L("guild_expand_limited"))
        UIHandler:addPopDialog(messageBox)
    end
end


function ClassRef:onOperate(op, data)
    print("NewguildController:onOperate op = " .. op)
    if op == "selecte_shownOffLine" then -- 切换显示离线成员？
        if not self:isViewAlive() then return end
        if 1 ~= self._curId then return end

        local searchText = self._view:getPanelById(1):getSearchText()
        if searchText and "" ~= searchText then -- 存在搜索内容？
            local memberList = self._model:getGuildMembersByName( searchText, data )
            self:refreshGuildMemberList( nil, memberList )
        else
            self:refreshGuildMemberList( data )
        end
    elseif op == "search_guildMember" then -- 搜索公会成员
        dump(data)
        -- print("###########      搜索公会成员    ",data[1])
        if app:isNeedCheckInput() then
            app:checkInput(data[1], function(response,errorMsg,code)
                if code == 200 then
                    if response and response.result then
                        local memberList = self._model:getGuildMembersByName( data[1], data[2] )
                        self:refreshGuildMemberList( nil, memberList )
                    else
                        display.pushToast(L("lua_code_text_11"))
                    end
                else
                    display.pushToast(L("network_error"))
                end
            end)
        else
            local memberList = self._model:getGuildMembersByName( data[1], data[2] )
            self:refreshGuildMemberList( nil, memberList )
        end        
    elseif op == "closeSearch_guild" then -- 取消搜索公会成员
        self:refreshGuildMemberList()
    elseif op == "openDonate" then -- 捐献
        --增强捐献提示
        --贡献等级不足无法捐献提示
        --捐献次数不足提示 会长无限制 其他成员3次 读表
        local myGuildMemInfo = self._model:getMyGuildMemberInfo()
        local isLeader = myGuildMemInfo:isGuildLeader()

        if isLeader then
            --会长直接打开捐献界面 且无限制
            self:openDonateView()
        else
            local donateLv = myGuildMemInfo:getDonateLv()
            local donateLimitLv = tonumber(GD:getSystemPar("guildDonateLvLimit", 3))
            if donateLv >= donateLimitLv then
                --次数用光还是在捐献界面调整
                self:openDonateView()
            else
                --弱提示
                display.pushToast(string.format(L("guild_DonateLvLimit"),donateLimitLv))
            end
        end


    elseif op == "donate" then -- 确认捐献
        self._model:reqDonateGuild( data )
    elseif op == "guildManager_selectedPanel" then
        self._managerPanelCurIdx = data
        if data == 2 then
            self._model:reqGuildApplyList()
        end
    elseif op == "change_guild_notice" then -- 更改公会公告
        self._model:reqGuildNotice( "", data )
    elseif op == "change_guild_declare" then -- 更改公会招募宣言
        self._model:reqGuildNoticeDesc( data )
    elseif op == "applyRender_Op" then -- 处理申请者
        self._model:reqReplyGuildApply( data[2], data[1] )
    elseif op == "upMemberNumLimit" then -- 扩展人数上限
        self:_upGuildMemberNumLimit()
    elseif op == "getChallengeReward" then -- 领取公会挑战奖励
        app:sendMsg("BoxController", "openView", { boxType = Enums.BoxType.guildChallenge, guildChallengeID = data:getId() })
    elseif op == "preview_guildMemberInfo" then -- 查看公会成员信息
        if not self:isViewAlive() then return end
        local MaxDonateLv = GD:getMaxGuildDonateLv() -- 可捐献的最大等级
        if data[1]:getDonateLv() >= MaxDonateLv then
            self:showGuildTips(string.format(L("guild_donateLv"), data[1]:getDonateLv()), L("guild_DonateLv_MaxDesc"), data[2])
        else
            local guildDonateConf = GD:queryGuildDonateById( data[1]:getDonateLv())

            local onePart = tonumber(GD:getSystemPar("guildDonateGoldOnePart", 1 )) -- 一份多少钱
            local cost = ( tonumber(guildDonateConf.num ) - data[1]:getDonateCopies()) * onePart
            self:showGuildTips(string.format(L("guild_donateLv"), data[1]:getDonateLv()),  string.format( L("guild_DonateLv_Desc"), cost), data[2] )
        end
    elseif op == "guildMember_btnMore" then
        data.type = Enums.MenuType.guildMember
        UIHandler:showHeadMenu( data )
    elseif op == "wshisper_guildMemberInfo" then -- 私聊公会成员信息
        app:sendMsg("IMRoomController", "speakToTarget", data:getMemberInfo())
    elseif op == "team_guildMemberInfo" then -- 组队公会成员信息
        --组队
        if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_CREATE_TEAM) then
            if data:isOnline() then
                if app:sendMsg("TeamModel", "isInTeam", data:getMemberId()) then
                    display.pushToast(L("tips_3000323"))
                else
                    app:sendMsg("TeamModel", "reqInviteToTeam", data:getMemberId(), data:getMemberLevel())
                end
            else
                display.pushToast(L("tips_1050011"))
            end
        else
            display.showFunctionNotOpenDetail(Constant.FUNC_TYPE_CREATE_TEAM)
        end
    elseif op == "showTips_onLine" then -- 在线成员tips
        local myGuildInfo = self._model:getMyGuildInfo()
        local memberNum = myGuildInfo:getGuildOnlineMemberNum() or -1
        self:showGuildTips( L("guild_member_tipsTitle"), string.format( L("guild_member_tips"), memberNum ), data )
    elseif op == "showTips_Lv" then -- 公会等级tips
        local myGuildInfo = self._model:getMyGuildInfo()
        local curExp = myGuildInfo:getGuildExp()
        local guildLvConf = GD:queryGuildLvById( myGuildInfo:getGuildLv() )
        if tonumber(guildLvConf.exp) == 0 then
            self:showGuildTips( L("guild_level_tipsTitle"), L("guild_levelmax_tips"), data )
        else
            local expNeed = tonumber( guildLvConf.exp ) - curExp
            local guildExpDaily = GD:getSystemPar("guildExpDaily", 50)
            self:showGuildTips( L("guild_level_tipsTitle"), string.format( L("guild_level_tips"), expNeed, myGuildInfo:getTodayAddExp(), guildExpDaily), data )
        end
    elseif op == "showTips_donate" then -- 公会捐献资金tips
        self:showGuildTips( L("guild_fund_tipsTitle"), L("guild_fund_tips"), data )
    elseif op == "showTips_challenge" then
        self:showGuildTips( data[1], data[2], data[3] )
    elseif op == "showTips_guildSkillPoint" then
        self:showGuildTips( string.format(L("guild_skillpoint"), self._model:getMyGuildInfo():getGuildLastSkillPoint()), L("guild_getskillpoint"), data )
    elseif op == "study_guild_talent" then
        --激活天赋
        self:reqStudyGuildTalent(data[1], data[2])
    elseif op == "set_guild_talent_donatelv" then
        --设置天赋需求的贡献等级
        --print(data[1].." "..data[2])
        self._model:reqSetGuildTalentDonateLv(data[1],data[2])
    elseif op == "show_guildtalent_donatelv_tips" then
        local str1 = string.format(L("guild_memberlevel"),data[2])
        local str2 = string.format(L("guild_needmemberlevel"),data[1])
        local str3 = L("guild_getmemberlevel")
        local tmpData = {}
        tmpData.string1 = str1
        tmpData.string3 = str3
        tmpData.name = str2
        self:_showRareTips(tmpData,data[3])
    elseif op == "grandTitle" then
        self._model:conferMemberSubTitle(data[1],data[2])
    elseif op == "revokeTitle" then
        self._model:cancelMemberSubTitle(data)
    elseif op == "showPeerageTips" then
        self:showPeerageTips(data[1],data[2])
    elseif op == "showGuildShop" then
        if not cc.isDead(self._view) then
            local panel = self._view:getPanelById(5)
            if panel then
                panel:setShopId(72071)
                panel:setSelectItemInfo({self._itemType, self._itemId})
                self._itemType = nil
                self._itemId = nil
                panel:updateLeftPanel()
            end
        end
    elseif op == "showMiniKeyboard" then
        self:_openKeyBoard(data[1],data[2],data[3])
    elseif op == "closeMiniKeyboard" then
        if not cc.isDead( self._keyboard ) then
            self._keyboard:removeFromParent(true)
            self._keyboard = nil
        end
    elseif op == "changeApplyMode" then --改变公会的审批模式
        self._model:reqChangeApplyMode(data[1],data[2],data[3])
    elseif op == "showEmperorBookView" then
        self:openEmperorBookView()
    end
end


function ClassRef:reqChangePeerage(uId)
    if cc.isDead( self._elevatePeerageView ) then
        self._elevatePeerageView = ElevatePeerageView.new()
        self._elevatePeerageView:onCloseView(handler(self, self.closeElevatePeerageView))
        self._elevatePeerageView:onClearView(handler(self, self.clearElevatePeerageView))
        self._elevatePeerageView:onOperate(handler(self, self.onOperate))
        self._view:addChild(self._elevatePeerageView)
    end
    self._elevatePeerageView:setData(self._model:getGuildMemberByUid(uId), self._model:getMyGuildInfo())
    self._elevatePeerageView:show()
end


function ClassRef:closeElevatePeerageView(data)
    if not cc.isDead( self._elevatePeerageView ) then
        self._elevatePeerageView:removeFromParent(true)
        self._elevatePeerageView = nil
    end
end
function ClassRef:clearElevatePeerageView()
    if self._elevatePeerageView then
        self._elevatePeerageView = nil
    end
end

function ClassRef:notiftGuildInvite(data)
    local confirmDialog = display.newConfirmDialog( string.format( L("lua_code_text_158"), data.name, data.guildName), 
        function(sender)
            self._model:reqApplyGuild(data.guildId)
        end)
    UIHandler:addPopDialog(confirmDialog)
end

--回来刷新授爵界面
function ClassRef:refreshElevateView(data)
    if not cc.isDead( self._elevatePeerageView ) then
        self._elevatePeerageView:refreshView()
        if data[1] then
            self._elevatePeerageView:playGrantAni(data[2])
        end
    end
end


-- 授爵界面
function ClassRef:showDynastyPromoteView(data)
    UIHandler:removePopupUiByKey(DynastyPromoteView:getViewName())

    local view = DynastyPromoteView.new(nil,data)
    --dump(data)
    view:onCloseView(function(sender)
        UIHandler:removePopupUiByKey(sender:getViewName())

        -- 通知移除桌面tips
        local params = {
            remindId = Constant.FUNC_TYPE_DYNASTY_PROMOTE,
            dataTemp = data
        }
        UIHandler:getDesktopController():removeFourthTips( params )
    end)

    UIHandler:addPopupUi(view, view:getViewName())
    view:show()
end

-- 光辉历史界面
function ClassRef:showDynastyHistoryView(data)
    UIHandler:removePopupUiByKey(DynastyHistoryView:getViewName())

    local guildInfo = self._model:getMyGuildInfo()

    local view = DynastyHistoryView.new(nil,data)
    --view:setGuildInfo(guildInfo)
    view:onCloseView(function(sender)
        UIHandler:removePopupUiByKey(sender:getViewName())

        -- 通知移除桌面tips
        local params = {
            remindId = Constant.FUNC_TYPE_DYNASTY_HISTORY,
            dataTemp = data
        }
        UIHandler:getDesktopController():removeFourthTips( params )
    end)

    UIHandler:addPopupUi(view, view:getViewName())
    view:show()
end

function ClassRef:showPeerageTips(peerageId, pos)
    if cc.isDead( self._peerageTipsView ) then
        self._peerageTipsView = PeerageTips.create(nil,peerageId)
        self._view:addChild(self._peerageTipsView)

        display.nodeRemoveForNotHit(self._peerageTipsView, function()
            if self._peerageTipsView then
                self._peerageTipsView:removeFromParent(true)
                self._peerageTipsView = nil
            end
        end)
    end


    local width,height = self._peerageTipsView:getContentSize2()
    local posX = pos.x
    if posX > display.width - 50 then
        posX = display.width - 50
    end
    self._peerageTipsView:setPosition( cc.p(posX, display.height/2 + height/2 ) )
end

---------show emperor book view----------

function ClassRef:openEmperorBookView(data)
    if cc.isDead( self._emperorBookView ) then
        self._emperorBookView = NewguildEmperorBookView.new(app.mvc.VIEWTYPE_BLUR)
        self._emperorBookView:onCloseView(handler(self, self.closeEmperorBookView))
        self._emperorBookView:onClearView(handler(self, self.clearEmperorBookView))
        self._emperorBookView:onOperate(handler(self, self.onOperate))
    end
    self:addChild(self._emperorBookView)
    self._emperorBookView:show()
end
function ClassRef:closeEmperorBookView(data)
    if not cc.isDead( self._emperorBookView ) then
        self:removeChild(self._emperorBookView)
    end
end
function ClassRef:clearEmperorBookView()
    if self._emperorBookView then
        self._emperorBookView = nil
    end
end
---------show emperor book view----------

--公会商店相关
function ClassRef:updateItem()
    if not self:isViewAlive() then return end
    self._view:updateGoldInfo()
end

-------------------keyboard-----------------------------
function ClassRef:_openKeyBoard(pos,num,callFunc)
    if cc.isDead( self._keyboard ) then
        self._keyboard = AuctionKeyboardPanel.create()
        self._keyboard:setCallFunc(callFunc)
        self._keyboard:setNum(num)
        self._keyboard:onOperate(handler(self, self.onOperate))
        UIHandler:addPopupUi( self._keyboard,"GuildKeyboardPanel")
        -- 点击空白区域关闭
        display.nodeRemoveForNotHit(self._keyboard, function()
            if not cc.isDead( self._keyboard ) then
                self._keyboard:performWithDelay(function()
                    UIHandler:removePopupUiByKey("GuildKeyboardPanel")
                    self._keyboard = nil
                end, 0)
            end
        end)
    end

    self._keyboard:setVisible( true )

    local width = self._keyboard:getContentSize2()
    local posX = pos.x
    if posX + width > display.width then
        posX = display.width - width
    end
    self._keyboard:setPosition( cc.p( posX, pos.y ) )
end

--------------------endkeyboard---------------------------------

return ClassRef
